package com.tenton.controller;

import com.tenton.MyException.DataNotExist;
import com.tenton.MyException.InsufficientQuantity;
import com.tenton.pojo.Sales;
import com.tenton.service.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @Date: 2021/1/9
 * @Author: Tenton
 * @Description:
 */
@ApiIgnore
@Controller
@ApiOperation("管理员商品销售管理")
@RequestMapping("/admin/flatform/sales")
public class ASalesController {
    @Autowired
    SalesService salesService;
    @Autowired
    StockService stockService;
    @Autowired
    CommodityService commodityService;
    @Autowired
    DailySalesService dailySalesService;
    @Autowired
    MonthlySalesService monthlySalesService;

    /**
     * 跳转到增加页面
     *
     * @return
     */
    @ApiOperation("跳转到增加页面")
    @GetMapping("/toAdd")
    public String toAdd() {
        return "admin/sales/addSales";

    }

    /**
     * 先判断数据库有没有，没有就新增
     *
     * @param sale
     * @param session
     * @param request
     * @return
     */
    @ApiOperation("新增销售记录")
    @PostMapping("/insert")
    public String insert(Sales sale, HttpSession session, HttpServletRequest request) {
        try {
            salesService.insert(sale,session);
            return "redirect:/admin/flatform/sales/getAll";
        } catch (InsufficientQuantity insufficientQuantity) {
            request.setAttribute("msg", "商品库存不足，请重新输入！");
            return "admin/sales/addSales";
        } catch (DataNotExist dataNotExist) {
            request.setAttribute("msg", "销售信息为空！");
            return "admin/sales/addSales";
        }
    }

    /**
     * 删除销售记录
     *
     * @param id
     * @param session
     * @return
     */
    @ApiOperation("通过Id删除销售记录")
    @GetMapping("/delete")
    public String delete(int id,HttpSession session) {
        salesService.deleteByPrimaryKey(id,session);
        return "redirect:/admin/flatform/sales/getAll";
    }

    /**
     * 获取所有销售信息
     *
     * @param model
     * @param pn
     * @return
     */
    @ApiOperation("获取所有销售信息")
    @GetMapping("/getAll")
    public String getAll_sale(ModelMap model,
                              @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn
    ) {
        salesService.getAll(pn, model);
        return "admin/sales/getSalesList";

    }

    /**
     * 查询销售记录
     *
     * @param id
     * @param model
     * @return
     */
    @ApiOperation("根据Id查询销售记录")
    @GetMapping("/getSales")
    public String getSales(int id, Model model) {
        salesService.selectByPrimaryKey(id, model);
        return "admin/sales/getSale";

    }

    /**
     * 查询商品信息
     *
     * @param comId
     * @param model
     * @return
     */
    @ApiOperation("通过comId查询商品信息")
    @GetMapping("/getCommodityByComId")
    public String getCommodityByComId(int comId, Model model, HttpServletRequest request) {
        try {
            commodityService.getCommodityByComId(comId, model);
        } catch (DataNotExist dataNotExist) {
            request.setAttribute("msg", "商品信息不存在！");
        }
        return "admin/sales/addSales";
    }

    /**
     * 商品名称模糊查询
     *
     * @param model
     * @param comName
     * @param pn
     * @return
     */
    @ApiOperation("商品名称模糊查询")
    @GetMapping("/getByParams")
    public String getByParams(ModelMap model,
                              @ApiParam("商品名称") @RequestParam(value = "comName", required = false) String comName,
                              @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn
    ) {
        salesService.getByParams(comName, pn, model);
        return "admin/sales/getSalesByParams";
    }

    /**
     * 跳转到商品销售统计页面
     *
     * @return
     */
    @ApiOperation("跳转到商品销售统计页面")
    @GetMapping("/toechart")
    public String toechart() {
        return "admin/echart";
    }

    @PostMapping(value = "/echartsData")
    @ResponseBody
    public List<Map<String, Object>> initChart() {
        return salesService.pieData();
    }

    /**
     * 跳转到商品日销售统计页面
     *
     * @return
     */
    @GetMapping("/toDailyEChart")
    public String toDailyEChart() {
        return "admin/dailyEChart";
    }

    @PostMapping("/dailyEChartsData")
    @ResponseBody
    public Map<String,Object> initDailyChart() {
        return dailySalesService.polyDate();
    }

    /**
     * 跳转到商品日销售统计页面
     *
     * @return
     */
    @GetMapping("/toMonthlyEChart")
    public String toMonthlyEChart() {
        return "admin/monthlyEChart";
    }

    @PostMapping("/monthlyEChartsData")
    @ResponseBody
    public Map<String,Object> initMonthlyChart() {
        return monthlySalesService.polyDate();
    }
}
